/*
 * HSM Proxy Project.
 * Copyright (C) 2013 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see 
 * http://www.gnu.org/licenses/.
 */

package be.fedict.hsm.jca;

import java.security.KeyStore;
import java.security.KeyStore.ProtectionParameter;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;

import be.fedict.hsm.client.HSMProxyClient;

/**
 * HSM Proxy key store load parameter. Must be used to load an HSMProxy key
 * store. Usage:
 * 
 * <pre>
 * KeyStore keyStore = KeyStore.getInstance(&quot;HSMProxy&quot;);
 * HSMProxyKeyStoreParameter keyStoreParameter = new HSMProxyKeyStoreParameter(
 * 		credentialPrivateKey, credentialCertificate,
 * 		&quot;http://localhost:8080/hsm-proxy-ws/dss&quot;);
 * keyStore.load(keyStoreParameter);
 * </pre>
 * 
 * @author Frank Cornelis
 * 
 */
public class HSMProxyKeyStoreParameter implements KeyStore.LoadStoreParameter {

	private final PrivateKey credentialPrivateKey;

	private final X509Certificate credentialCertificate;

	private final String hsmProxyEndpointAddress;

	private final HSMProxyAudit hsmProxyAudit;

	private final HSMProxyClient hsmProxyClient;

	/**
	 * 
	 * @param credentialPrivateKey
	 * @param credentialCertificate
	 * @param hsmProxyEndpointAddress
	 */
	public HSMProxyKeyStoreParameter(PrivateKey credentialPrivateKey,
			X509Certificate credentialCertificate,
			String hsmProxyEndpointAddress) {
		this(credentialPrivateKey, credentialCertificate,
				hsmProxyEndpointAddress, null);
	}

	/**
	 * Main constructor.
	 * 
	 * @param credentialPrivateKey
	 *            the credential private key.
	 * @param credentialCertificate
	 *            the corresponding credential X509 certificate.
	 * @param hsmProxyEndpointAddress
	 *            the HSM Proxy web service endpoint address.
	 * @param hsmProxyAudit
	 *            the optional audit component.
	 */
	public HSMProxyKeyStoreParameter(PrivateKey credentialPrivateKey,
			X509Certificate credentialCertificate,
			String hsmProxyEndpointAddress, HSMProxyAudit hsmProxyAudit) {
		this.credentialPrivateKey = credentialPrivateKey;
		this.credentialCertificate = credentialCertificate;
		this.hsmProxyEndpointAddress = hsmProxyEndpointAddress;
		this.hsmProxyAudit = hsmProxyAudit;
		this.hsmProxyClient = new HSMProxyClient(this.hsmProxyEndpointAddress,
				this.credentialPrivateKey, this.credentialCertificate);
	}

	@Override
	public ProtectionParameter getProtectionParameter() {
		return null;
	}

	public PrivateKey getCredentialPrivateKey() {
		return this.credentialPrivateKey;
	}

	public X509Certificate getCredentialCertificate() {
		return this.credentialCertificate;
	}

	public String getHsmProxyEndpointAddress() {
		return this.hsmProxyEndpointAddress;
	}

	public HSMProxyAudit getHsmProxyAudit() {
		return this.hsmProxyAudit;
	}

	public HSMProxyClient getHSMProxyClient() {
		return this.hsmProxyClient;
	}

	/**
	 * Sets the HTTP proxy to be used to contact the HSM Proxy web service.
	 * 
	 * @param proxyHost
	 *            the HTTP proxy host address.
	 * @param proxyPort
	 *            the HTTP proxy port number.
	 */
	public void setProxy(String proxyHost, int proxyPort) {
		this.hsmProxyClient.setProxy(proxyHost, proxyPort);
	}
}
